<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller {
    
    private $model='user';
    private $table='admin';
    
	function __construct()
    {
        parent::__construct();
		$this->check_manage_session();//判断用户是否登录
    }
	
	public function index()
	{
		$this->page();
	}
	
	//列表方法
	public function page()
	{
		
		$page=5;
		$return=$this->mypage($this->model,$this->table,$page);
		$data['list'] = $this->admin_model->get_list($this->table,$return['pagesize'], $return['offset'],'','id desc');
		$data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
		$this->load->view($this -> config->item('system_manage_path').'user_list',$data);
	}
		
	public function add()
	{
	    $data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
	    $this->load->view($this -> config->item('system_manage_path').'user_add',$data);
	}
	
	public function edit($id){
	    $data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
		$data['show'] = $this->admin_model->get_show($this->table,$id);
		$this->load->view($this -> config->item('system_manage_path').'user_edit',$data);
	}
	
	public function save($action=NULL,$id=NULL){
		if (empty($action)) {
		    $action=$this->input->post('action');
		}
		if (empty($id)) {
		    $id=$this->input->post('id');
		}
		
		$post=$this->input->post();
		if(!empty($post['password'])){
		    $post['password']=md5($post['password']);
		    $post['repassword']=md5($post['repassword']);
		}
		$post=unset_array($post,'lcomeurl'); //过滤无效表单项
		$data=set_array($post);
		
		$lcomeurl=get_comurl($this->input->post('lcomeurl'));
		
		if($action=='add'){
		    $action_text="添加用户";
			if(empty($post['adminname'])){
				$this->error('用户名不能为空',2);
			}
			if(empty($post['password'])){
				$this->error('密码不能为空',2);
			}else{
			    if($post['password'] != $post['repassword']){
			        $this->error('两次输入的密码不一致',2);
			    }
			}
			$data=unset_array($data,'repassword'); //过滤无效表单项
			$result=$this->admin_model->add($this->table,$data);
		}elseif($action=='edit'){
		    $action_text="修改用户";
			if(empty($id)){
				$this->error('参数错误',2);
			}
			if(!empty($post['password'])){
				if($post['password'] != $post['repassword']){
					$this->error('两次输入的密码不一致',2);
				}
			    $data=unset_array($data,'repassword,createtime'); //过滤无效表单项
			}else{
			    $data=unset_array($data,'password','repassword,createtime'); //过滤无效表单项
			}
			
			$result=$this->admin_model->edit($this->table,$data,$id);
		}elseif($action=='del'){
		    $action_text="删除用户";
			if(empty($id)){
				$this->error('请选择要操作的对象',2);
			}
			foreach ($id as $value) {
			    $result=$this->admin_model->del($this->table,$value);
			}
		}elseif($action=='lock'){
		    $action_text="锁定用户";
			if(empty($id)){
				$this->error('请选择要操作的对象',2);
			}
			foreach ($id as $value) {
                $result=$this->admin_model->set_field($this->table,'status','0',$value);
			}
		}elseif($action=='unlock'){
		    $action_text="解锁用户";
			if(empty($id)){
				$this->error('请选择要操作的对象',2);
			}
			foreach ($id as $value) {
                $result=$this->admin_model->set_field($this->table,'status','1',$value);
			}
		}
		if($result){
		    $lcomeurl=str_replace("welcome", "user", $lcomeurl);
			$this->success($action_text.'成功',2,$lcomeurl);
		}else{
			$this->error($action_text.'失败',2);
		}
	}
	
}
